package com.yyplatform.admin.platform.generator.service.impl;

import java.util.List;

import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yyplatform.admin.platform.generator.model.dto.ColumnConfigDTO;
import com.yyplatform.admin.platform.generator.model.dto.ColumnConfigFormDTO;
import com.yyplatform.admin.platform.generator.model.dto.ColumnConfigQueryDTO;
import com.yyplatform.admin.platform.generator.model.entity.GenColumnConfigEntity;
import com.yyplatform.admin.platform.generator.model.entity.GenTableConfigEntity;
import com.yyplatform.admin.platform.generator.helper.MySQLSchemaHelper;
import com.yyplatform.admin.platform.generator.model.ColumnVO;
import com.yyplatform.admin.platform.generator.repository.GenColumnConfigRepository;
import com.yyplatform.admin.platform.generator.repository.GenTableConfigRepository;
import com.yyplatform.admin.platform.generator.service.ColumnConfigService;
import com.yyplatform.admin.platform.generator.utils.ColumnFieldUtils;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-23
 */
@Service
@Slf4j
@AllArgsConstructor
public class ColumnConfigServiceImpl implements ColumnConfigService {

    private GenColumnConfigRepository columnConfigRepository;
    private GenTableConfigRepository tableConfigRepository;
    private ModelMapper modelMapper;
    private MySQLSchemaHelper mySQLSchemaHelper;


    @Override
    public List<ColumnConfigDTO> query(ColumnConfigQueryDTO queryDTO) {
        List<GenColumnConfigEntity> entityList =
                columnConfigRepository.findByTableConfigId(queryDTO.getTableConfigId());
        GenTableConfigEntity tableConfigEntity = tableConfigRepository.getReferenceById(queryDTO.getTableConfigId());

        if (entityList == null || entityList.isEmpty()) {
            initColumnList(tableConfigEntity);
            entityList = columnConfigRepository.findByTableConfigId(queryDTO.getTableConfigId());
        }

        return entityList.stream().map(e -> modelMapper.map(e, ColumnConfigDTO.class)).toList();
    }

    private void initColumnList(GenTableConfigEntity tableConfigEntity) {
        List<ColumnVO> columnVOList = mySQLSchemaHelper.listColumn(tableConfigEntity.getTableName());
        columnVOList.forEach(columnVO -> {
            GenColumnConfigEntity entity = new GenColumnConfigEntity();

            entity.setTableConfigId(tableConfigEntity.getId());
            entity.setTableName(tableConfigEntity.getTableName());

            entity.setColumnLength(
                    columnVO.getCharacterMaximumLength() == null ? 0 : columnVO.getCharacterMaximumLength().intValue());

            entity.setColumnName(columnVO.getColumnName());
            entity.setColumnType(columnVO.getColumnType());
            entity.setDictCode("");
            entity.setFieldComment(columnVO.getColumnComment());
            entity.setFieldName(ColumnFieldUtils.getFiledName(columnVO.getColumnName()));
            entity.setFieldType(ColumnFieldUtils.getFieldType(columnVO.getColumnType()));
            entity.setFieldSortNum(0);
            entity.setFormType(0);
            entity.setIsRequired(false);
            entity.setIsShowInList(false);
            entity.setIsShowInForm(false);
            entity.setQueryType(0);
            entity.setMaxLength(
                    columnVO.getCharacterMaximumLength() == null ? 0 : columnVO.getCharacterMaximumLength().intValue());

            columnConfigRepository.save(entity);
        });

    }


    @Transactional
    @Override
    public void saveAll(List<ColumnConfigFormDTO> formDTOList) {
        log.info(" fromDTOList.size ====>" + formDTOList.size());

        formDTOList.forEach(dto -> {
            GenColumnConfigEntity entity = columnConfigRepository.getReferenceById(dto.getId());
            // todo
            entity.setFieldName(dto.getFieldName());
            entity.setFieldComment(dto.getFieldComment());
            entity.setMaxLength(dto.getMaxLength());
            entity.setIsShowInQuery(dto.getIsShowInQuery());
            entity.setIsShowInList(dto.getIsShowInList());
            entity.setIsShowInForm(dto.getIsShowInForm());
            entity.setIsRequired(dto.getIsRequired());
            entity.setQueryType(dto.getQueryType());
            entity.setDictCode(dto.getDictCode());
            entity.setFormType(dto.getFormType());


            columnConfigRepository.save(entity);
        });
    }

    @Override
    public ColumnConfigDTO get(Long id) {
        GenColumnConfigEntity entity = columnConfigRepository.getReferenceById(id);
        return modelMapper.map(entity, ColumnConfigDTO.class);
    }

    @Override
    public void save(ColumnConfigFormDTO dto) {
        GenColumnConfigEntity entity = modelMapper.map(dto, GenColumnConfigEntity.class);
        columnConfigRepository.save(entity);
    }

    @Override
    public void update(ColumnConfigFormDTO dto) {
        GenColumnConfigEntity entity = modelMapper.map(dto, GenColumnConfigEntity.class);
        columnConfigRepository.save(entity);
    }

    @Override
    public void delete(Long id) {
        columnConfigRepository.deleteById(id);
    }

}
